package com.app.lxx.pregnancyberth.ui.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.app.lxx.pregnancyberth.R;
import com.app.lxx.pregnancyberth.ui.entity.SmartHomeEntity;
import com.app.lxx.pregnancyberth.widge.NumberSeekBar;
import com.app.lxx.pregnancyberth.widge.UtilsManage;
import com.zjy.portdemo.ColorPickerView;

import java.util.List;

import static com.app.lxx.pregnancyberth.MyApplication.modbusMaster;

/**
 * Created by Lxx on PregnancyBerth
 * 2019/9/4 17:12 星期三
 */

public class SmartHomeAdapter extends RecyclerView.Adapter {

    private int TYPE_KT = 1, TYPE_CL = 2, TYPE_WL = 3, TYPE_DS = 4, TYPE_DG = 5, TYPE_XF = 6;

    private List<SmartHomeEntity.DataBean.IntelligentDataBean> listBeans;
    private Activity context;

    public SmartHomeAdapter(Activity context, List<SmartHomeEntity.DataBean.IntelligentDataBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
    }

    private OnClickItemListener onClickItemListener;

    public interface OnClickItemListener {
        void onClickListener(int position);
    }

    public void setOnClickListener(OnClickItemListener clickListener) {
        onClickItemListener = clickListener;
    }


    @Override
    public int getItemViewType(int position) {
        SmartHomeEntity.DataBean.IntelligentDataBean intelligentDataBean = listBeans.get(position);
        String name = intelligentDataBean.getInfo().getName();
        if (name.equals("智能空调")) {
            return TYPE_KT;
        } else if (name.equals("智能窗帘")) {
            return TYPE_CL;
        } else if (name.equals("智能围帘")) {
            return TYPE_WL;
        } else if (name.equals("智能电视")) {
            return TYPE_DS;
        } else if (name.equals("新风控制")) {
            return TYPE_XF;
        } else {
            return TYPE_DG;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view;
        if (viewType == TYPE_KT) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_kt, viewGroup, false);
            return new MyViewHolderKt(view);
        } else if (viewType == TYPE_CL) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_cl, viewGroup, false);
            return new MyRecmendViewHolder(view);
        } else if (viewType == TYPE_WL) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_wl, viewGroup, false);
            return new MyViewHolderCl(view);
        } else if (viewType == TYPE_DS) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_ds, viewGroup, false);
            return new MyViewHolderCl(view);
        } else if (viewType == TYPE_DG) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_dg, viewGroup, false);
            return new MyViewHolderCl(view);
        } else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_smart_xf, viewGroup, false);
            return new MyViewHolderCl(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof MyViewHolderKt) {

        } else if (viewHolder instanceof MyViewHolderCl) {

        } else if (viewHolder instanceof MyViewHolderWl) {

        } else if (viewHolder instanceof MyViewHolderDs) {

        } else if (viewHolder instanceof MyViewHolderDg) {


        } else if (viewHolder instanceof MyViewHolderXf) {

        }

        if (getItemViewType(position) == TYPE_DG) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("viewHolder", "ssssssss");
                    //  onClickItemListener.onClickListener(position);

                }
            });
//            ((MyViewHolderDg) viewHolder).dengguang.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            });
        }

    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }

    private class MyRecmendViewHolder extends RecyclerView.ViewHolder {
        public MyRecmendViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    private class MyViewHolderKt extends RecyclerView.ViewHolder {


        public MyViewHolderKt(@NonNull View itemView) {
            super(itemView);

        }
    }

    private class MyViewHolderCl extends RecyclerView.ViewHolder {


        public MyViewHolderCl(@NonNull View itemView) {
            super(itemView);

        }
    }

    private class MyViewHolderWl extends RecyclerView.ViewHolder {

        public MyViewHolderWl(@NonNull View itemView) {
            super(itemView);

        }
    }

    private class MyViewHolderDs extends RecyclerView.ViewHolder {

        public MyViewHolderDs(@NonNull View itemView) {
            super(itemView);

        }
    }

    private class MyViewHolderDg extends RecyclerView.ViewHolder {

        ImageView smart_dg_iv;
        View dengguang, dengguangly;
        NumberSeekBar NumberSeekBar1, NumberSeekBar2;
        SeekBar seekBar;

        public MyViewHolderDg(@NonNull View itemView) {
            super(itemView);
            smart_dg_iv = itemView.findViewById(R.id.smart_dg_iv);
            dengguang = itemView.findViewById(R.id.dengguang);
            dengguangly = itemView.findViewById(R.id.dengguangly);
            NumberSeekBar1 = itemView.findViewById(R.id.NumberSeekBar1);
            NumberSeekBar2 = itemView.findViewById(R.id.NumberSeekBar2);

            final SmartHomeEntity.DataBean.IntelligentDataBean intelligentDataBean = listBeans.get(getAdapterPosition());
            final SmartHomeEntity.DataBean.IntelligentDataBean.InfoBean info = intelligentDataBean.getInfo();
            if (info.getMode().contains("普通")) {
                dengguangly.setVisibility(View.GONE);
            } else {
                dengguangly.setVisibility(View.VISIBLE);
            }
            smart_dg_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("ssss", "close");
                    showColorPickerView();
                }
            });
            dengguang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("ssss", "close");
                    for (int i = 0; i < data.length; i++) {
                        data[i] = 0;
                    }
                }
            });

//            NumberSeekBar1.setOnSeekBarChang(new NumberSeekBar.OnSeekBarChang() {
//                @Override
//                public void seekBarChangs(float progress) {
//                    int progress1 = (int) progress;
//                    switch (info.getMode()) {
//                        case "色彩":
//                            data[4] = progress1 * 25;//冷色:0-255
//                            data[6] = progress1 * 10;//冷色dac:0-1000
//                            data[8] = progress1 * 10;//冷色pwm:0-1000
//                            modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
//                            break;
//                        case "色温":
//                            setTwoColor(false, progress1, 0);
//                            break;
//                        case "普通":
//                            setDanse(progress1);
//                            break;
//                    }
//                }
//            });
//            NumberSeekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//                @Override
//                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                    switch (info.getMode()) {
//                        case "色彩":
//                            data[3] = progress * 25;//暖色:0-255
//                            data[5] = progress * 10;//暖色dac:0-1000
//                            data[7] = progress * 10;//暖色pwm:0-1000
//                            modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
//                            break;
//                        case "色温":
//                            setTwoColor(true, 0, progress);
//                            break;
//                        case "普通":
//                            setDanse(progress);
//                            break;
//                    }
//                }
//
//                @Override
//                public void onStartTrackingTouch(SeekBar seekBar) {
//
//                }
//
//                @Override
//                public void onStopTrackingTouch(SeekBar seekBar) {
//
//                }
//            });

        }
    }

    private class MyViewHolderXf extends RecyclerView.ViewHolder {

        public MyViewHolderXf(@NonNull View itemView) {
            super(itemView);

        }
    }

    private ColorPickerView colorDisk = null;
    private TextView tv = null;
    private String address;
    int[] data = new int[9];


    private void showColorPickerView() {
        UtilsManage utilsManage = new UtilsManage(context);
        View inflate = LayoutInflater.from(context).inflate(R.layout.activity_lighting, null);
        final Dialog dialog = utilsManage.dialog(context, inflate, Gravity.BOTTOM, true);

        colorDisk = inflate.findViewById(R.id.colorDisk);
        tv = inflate.findViewById(R.id.tv);

        colorDisk.setOnColorBackListener(new ColorPickerView.OnColorBackListener() {
            @Override
            public void onColorBack(int a, int r, int g, int b) {
                tv.setText("R：" + r + "\nG：" + g + "\nB：" + b + "\n" + colorDisk.getColorStr());
                tv.setTextColor(Color.argb(a, r, g, b));

                /**
                 * 定义数组共5位
                 * 彩色灯光
                 */
                data[0] = r;//红色
                data[1] = g;//绿色
                data[2] = b;//蓝色
                data[3] = 0;//暖色:0-255
                data[4] = 0;//冷色:0-255

                data[5] = 0;//暖色dac:0-1000
                data[6] = 0;//冷色dac:0-1000
                data[7] = 0;//暖色pwm:0-1000
                data[8] = 0;//冷色pwm:0-1000
                /**
                 * 发送地址为0x01
                 * 固定位0x10
                 * 起始位为0
                 * data数据
                 */
                modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
            }
        });
    }


    private void setDanse(int num) {
        data[0] = 0;//红色
        data[1] = 0;//绿色
        data[2] = 0;//蓝色
        data[3] = 0;//暖色:0-255
        data[4] = 0;//冷色:0-255

        data[5] = num;//暖色dac:0-1000
        data[6] = num;//冷色dac:0-1000
        data[7] = num;//暖色pwm:0-1000
        data[8] = num;//冷色pwm:0-1000
        modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
    }

    private void setTwoColor(boolean type, int num1, int num2) {
        data[0] = 0;//红色
        data[1] = 0;//绿色
        data[2] = 0;//蓝色
        data[3] = 0;//暖色:0-255
        data[4] = 0;//冷色:0-255
        if (type) {
            data[5] = num2;//暖色dac:0-1000
            data[7] = num2;//暖色pwm:0-1000
        } else {
            data[6] = num1;//冷色dac:0-1000
            data[8] = num1;//冷色pwm:0-1000
        }
        modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
    }
}
